package com.heartware.sh.blackhole.adapter;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.heartware.sh.blackhole.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/24 0024.
 */
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    public static final int FIRST_STICKY_VIEW = 1;
    public static final int HAS_STICKY_VIEW = 2;
    public static final int NONE_STICKY_VIEW = 3;

    private List<String> strList = new ArrayList<String>();

    public MyAdapter(List<String> strList){
        this.strList = strList;
    }

    private MyItemClickListener itemClickListener;
    private MyItemLongClickListener itemLongClickListener;

    public void setItemClickListener(MyItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public void setItemLongClickListener(MyItemLongClickListener itemLongClickListener) {
        this.itemLongClickListener = itemLongClickListener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);

        MyViewHolder holder = new MyViewHolder(itemView,itemClickListener, itemLongClickListener);

        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        holder.tv.setText(strList.get(position));

        if (position == 0) {
            holder.tv_header.setVisibility(View.VISIBLE);
            holder.tv_header.setText(strList.get(position));
            holder.itemView.setTag(FIRST_STICKY_VIEW);
        } else {
            if (!TextUtils.equals(strList.get(position), strList.get(position - 1))) {
                holder.tv_header.setVisibility(View.VISIBLE);
                holder.tv_header.setText(strList.get(position));
                holder.itemView.setTag(HAS_STICKY_VIEW);
            } else {
                holder.tv_header.setVisibility(View.GONE);
                holder.itemView.setTag(NONE_STICKY_VIEW);
            }
        }
        holder.itemView.setContentDescription(strList.get(position)+"000");

    }

    @Override
    public int getItemCount() {
        return strList.size();
    }

    public interface MyItemClickListener{
        void onItemClick(View v, int position);
    }

    public interface MyItemLongClickListener{
        void onItemLongClick(View v, int position);
    }


    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{

        public TextView tv_header;
        public TextView tv;

        private MyItemClickListener clickListener;

        private MyItemLongClickListener longClickListener;

        public MyViewHolder(View itemView , MyItemClickListener clickListener, MyItemLongClickListener longClickListener) {
            super(itemView);

            tv_header = (TextView) itemView.findViewById(R.id.tv_sticky_header_view);
            tv = (TextView) itemView.findViewById(R.id.tv);

            this.clickListener = clickListener;
            this.longClickListener = longClickListener;

            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if(clickListener != null){
                clickListener.onItemClick(view, getPosition());
            }
        }

        @Override
        public boolean onLongClick(View view) {
            if(longClickListener != null){
                longClickListener.onItemLongClick(view, getPosition());
            }
            return true;
        }
    }

}
